package com.luck.easyfoodbatch.pickerview;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.util.Tools;

import java.util.Calendar;
import java.util.List;

public class PopBatchHelper {

    private Context context;
    private PopupWindow pop;
    private View view;
    private OnClickOkListener onClickOkListener;

    private List<String> listYear, listMonth, listDay;
    private String year, month, day;
    private String starTime = "";//

    public void setStarTime(String starTime) {
        this.starTime = starTime;
    }

    public PopBatchHelper(Context context) {
        this.context = context;
        view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.picker_birth, null);
        pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        initPop();
        initData();
        initView();
    }


    private void initPop() {
        pop.setAnimationStyle(android.R.style.Animation_InputMethod);
        pop.setFocusable(true);
        pop.setOutsideTouchable(true);
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }


    /**
     * 初始化数据
     */
    private void initData() {
        listYear = DatePackerUtil.getBirthYearList();
        listMonth = DatePackerUtil.getBirthMonthList();
        listDay = DatePackerUtil.getBirthDay31List();
    }

    private void initView() {
        Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
        Button btnOk = (Button) view.findViewById(R.id.btnOK);
        final LoopView loopView1 = (LoopView) view.findViewById(R.id.loopView1);
        final LoopView loopView2 = (LoopView) view.findViewById(R.id.loopView2);
        final LoopView loopView3 = (LoopView) view.findViewById(R.id.loopView3);
        loopView1.setList(listYear);
        loopView1.setNotLoop();
        final Calendar c = Calendar.getInstance();
        int monthIndex = c.get(Calendar.MONTH);
        int dayIndex = c.get(Calendar.DATE) - 1;
        loopView1.setCurrentItem(0);//定位到2016年
        loopView2.setList(listMonth);
        loopView2.setNotLoop();
        loopView2.setCurrentItem(monthIndex);

        loopView3.setList(listDay);
        loopView3.setNotLoop();
        loopView3.setCurrentItem(dayIndex);
        String s = listDay.get(19);
        System.out.print(s);
        loopView1.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                try {
                    String select_item = listYear.get(item);
                    if (TextUtils.isEmpty(year)) {
                        year = c.get(Calendar.YEAR) + "";
                    } else {
                        year = select_item.replace("年", "");
                    }
                    int newDay = c.get(Calendar.YEAR);
                    if (!Tools.isNull(year) && year.equals(newDay + "")) {
                        // 如果是当前年份
                        int m = c.get(Calendar.MONTH) + 1;
                        int oldM = loopView2.getCurrentItem();
                        String s1 = listMonth.get(oldM);
                        int nm = Integer.parseInt(s1.replaceAll("月", ""));
                        if (nm < m) {
                            loopView2.setCurrentItem(m - 1);
                        }
                    }
                    if (!TextUtils.isEmpty(month) && month.equals("2")) {
                        if (DatePackerUtil.isRunYear(year) && listDay.size() != 29) {
                            listDay = DatePackerUtil.getBirthDay29List();
                            loopView3.setList(listDay);
                            loopView3.setCurrentItem(0);
                        } else if (!DatePackerUtil.isRunYear(year) && listDay.size() != 28) {
                            listDay = DatePackerUtil.getBirthDay28List();
                            loopView3.setList(listDay);
                            loopView3.setCurrentItem(0);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        loopView2.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                try {
                    int dayIndex = 0;
                    String select_item = listMonth.get(item);
                    int m = c.get(Calendar.MONTH) + 1;
                    int newDay = c.get(Calendar.YEAR);

                    int monthIndex = c.get(Calendar.MONTH) + 1;
                    if (TextUtils.isEmpty(month)) {
                        int i = c.get(Calendar.MONTH) + 1;
                        dayIndex = c.get(Calendar.DATE) - 1;
                        month = i + "";
                    } else {
                        month = select_item.replace("月", "");
                    }

                    if (Tools.isNull(month)) {
                        month = c.get(Calendar.MONTH) + 1 + "";
                    }

                    if (!Tools.isNull(year) && year.equals(newDay + "")) {
                        // 如果是当前年份
                        int oldM = Integer.parseInt(month);
                        if (oldM < m) {
                            loopView2.setCurrentItem(m - 1);
                        }
                    }

                    if (month.equals("2")) {
                        if (!TextUtils.isEmpty(year) && DatePackerUtil.isRunYear(year) && listDay.size() != 29) {
                            listDay = DatePackerUtil.getBirthDay29List();
                            loopView3.setList(listDay);
                            loopView3.setCurrentItem(dayIndex);
                        } else if (!TextUtils.isEmpty(year) && !DatePackerUtil.isRunYear(year) && listDay.size() != 28) {
                            listDay = DatePackerUtil.getBirthDay28List();
                            loopView3.setList(listDay);
                            if (!Tools.isNull(year) && year.equals(newDay + "")) {
                                int currentItem = loopView3.getCurrentItem();
                                int newD = c.get(Calendar.DATE);
                                if (currentItem < newD) {
                                    loopView3.setCurrentItem(newD - 1);
                                }
                            } else {
                                if (Integer.parseInt(month) == monthIndex) {
                                    // 如果当前月份 设置为当前日期
                                    loopView3.setCurrentItem(c.get(Calendar.DATE) - 1);
                                } else {
                                    loopView3.setCurrentItem(dayIndex);
                                }
                            }
                        }
                    } else if (month.equals("1") || month.equals("3") || month.equals("5") || month.equals("7") || month.equals("8") || month.equals("10") || month.equals("12") && listDay.size() != 31) {
                        listDay = DatePackerUtil.getBirthDay31List();
                        loopView3.setList(listDay);
                        if (!Tools.isNull(year) && year.equals(newDay + "")) {
                            int currentItem = loopView3.getCurrentItem();
                            int newD = c.get(Calendar.DATE);
                            if (currentItem < newD) {
                                loopView3.setCurrentItem(newD - 1);
                            }
                        } else {
                            if (Integer.parseInt(month) == monthIndex) {
                                // 如果当前月份 设置为当前日期
                                loopView3.setCurrentItem(c.get(Calendar.DATE) - 1);
                            } else {
                                loopView3.setCurrentItem(dayIndex);
                            }
                        }
                    } else if (month.equals("4") || month.equals("6") || month.equals("9") || month.equals("11") && listDay.size() != 30) {
                        listDay = DatePackerUtil.getBirthDay30List();
                        loopView3.setList(listDay);
                        if (!Tools.isNull(year) && year.equals(newDay + "")) {
                            int currentItem = loopView3.getCurrentItem();
                            int newD = c.get(Calendar.DATE);
                            if (currentItem < newD) {
                                loopView3.setCurrentItem(newD - 1);
                            }
                        } else {
                            if (Integer.parseInt(month) == monthIndex) {
                                // 如果当前月份 设置为当前日期 不是当前年
                                if (Tools.isNull(year) && year.equals(newDay + "")) {
                                    loopView3.setCurrentItem(c.get(Calendar.DATE) - 1);
                                } else {
                                    loopView3.setCurrentItem(dayIndex);
                                }
                            } else {
                                loopView3.setCurrentItem(dayIndex);
                            }
                        }

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        loopView3.setListener(new LoopListener() {
            @Override
            public void onItemSelect(int item) {
                try {
                    int newDay = c.get(Calendar.YEAR);
                    int m = c.get(Calendar.MONTH) + 1;
                    if (Tools.isNull(month)) {
                        month = m + "";
                    }

                    String select_item = listDay.get(item);
                    if (TextUtils.isEmpty(day)) {
                        int i = c.get(Calendar.DATE);
                        day = i + "";
                    } else {
                        day = select_item.replace("日", "");
                    }

                    if (!Tools.isNull(year) && year.equals(newDay + "")) {
                        int selectD = Integer.parseInt(day);// 当前日期
                        // 如果 选择的月份 大于当前月份
                        if (Integer.parseInt(month) > m) {

                        } else {
                            int newD = c.get(Calendar.DATE);
                            if (selectD < newD) {
                                loopView3.setCurrentItem(newD - 1);
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onClickOkListener.onClickOk(year + "-" + month + "-" + day);
                        pop.dismiss();
                    }
                }, 100);
            }
        });
    }

    /**
     * 显示
     *
     * @param view
     */
    public void show(View view) {
        pop.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }

    /**
     * 隐藏监听
     *
     * @param onDismissListener
     */
    public void setOnDismissListener(PopupWindow.OnDismissListener onDismissListener) {
        pop.setOnDismissListener(onDismissListener);
    }

    public void setOnClickOkListener(OnClickOkListener onClickOkListener) {
        this.onClickOkListener = onClickOkListener;
    }

    public interface OnClickOkListener {
        public void onClickOk(String birthday);
    }

}
